package com.sky.config;

import java.util.*;
import java.util.stream.Collectors;

interface MyInterface{
    public void doSomething(String s);
}

public class Lambda {

    // 7、interface
    public static void main(String[] args) {
        MyInterface myInterface = new MyInterface() {
            @Override
            public void doSomething(String s) {
                System.out.println(s);
            }
        };
        myInterface.doSomething("Hello world");

        MyInterface myInterface1 = (s) -> System.out.println(s);
        myInterface1.doSomething("Hello world");

        // 8、线程
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello world1");
            }
        });
        thread.start();

        Thread thread1 = new Thread(() -> System.out.println("Hello world1"));
        thread1.start();

        // 9、转大写
        String str = "hello world";
        if (str != null){
            System.out.println(str.toUpperCase());
        }

        Optional.ofNullable(str).map(String::toUpperCase).ifPresent(System.out::println);

        // 10、流水线操作
        List<String> list1 = Arrays.asList("apple", "banana", "orange");
        List<String> list2 = new ArrayList<>();
        for (String s : list1) {
            if (s.startsWith("a")){
                list2.add(s.toUpperCase());
            }
        }
        System.out.println(list2);

        List<String> list3 = list1.stream().filter(s -> s.startsWith("a")).map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(list3);

        List<String> list = Arrays.asList("apple", "banana", "orange");

        // 1、for循环遍历改foreach
//        for (String s : list) {
//            System.out.println(s);
//        }

//        list.forEach(s -> {
//            System.out.println(s);
//        } );

        //list.forEach(System.out::println);// soutc

        // 2、集合比较大小来排序
//        Collections.sort(list, new Comparator<String>() {
//            @Override
//            public int compare(String o1, String o2) {
//                return o1.compareTo(o2);
//            }
//        });

//        Collections.sort(list, ((o1, o2) -> {
//            return o1.compareTo(o2);
//        }
//        ));
//        Collections.sort(list, ((o1, o2) -> o1.compareTo(o2)));

        // 3、把含有a的元素拿出来
//        List<String> list1 = new ArrayList<>();
//        for (String s : list1) {
//            if (s.startsWith("a")){
//                list1.add(s);
//            }
//        }
//
//        List<String> list2 = list.stream().filter(s -> s.startsWith("a")).collect(Collectors.toList());

        // 4、映射
//        List<Integer> list1 = new ArrayList<>();
//        for (String s : list) {
//            list1.add(s.length());
//        }
//
//        List<Integer> list2 = list.stream().map(s -> {
//            return s.length();
//        }).collect(Collectors.toList());
//        System.out.println(list1);
//        System.out.println(list2);

        // 5、
//        List<Integer> list1 = Arrays.asList(1, 2, 3,4,5);
//
//        int sum = 0;
//        for (Integer integer : list1) {
//            sum += integer;
//        }
//        System.out.println(sum);
//
//        int sum2 = list1.stream().reduce(0,(a,b) -> a + b);
//        System.out.println(sum2);

        // 6、map 根据长度分类
//        Map<Integer, List<String>> groups = new HashMap<>();
//        for (String s : list) {
//            int length = s.length();
//            if (!groups.containsKey(length)){
//                groups.put(length,new ArrayList<>());
//            }
//            groups.get(length).add(s);
//        }
//        System.out.println(groups);
//
//        Map<Integer,List<String>> groups1 = list.stream().collect(Collectors.groupingBy(s -> {
//            return s.length();
//        }));
//        Map<Integer,List<String>> groups2 = list.stream().collect(Collectors.groupingBy(String::length));
//        System.out.println(groups2);
    }
}
